package com.detection.hikvision.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.detection.hikvision.config.CusArtemisConfig;
import com.detection.hikvision.domain.RegionPageResultView;
import com.detection.hikvision.domain.RootRegionsResultView;
import com.detection.hikvision.service.ArtemisRegionsService;
import com.detection.hikvision.utils.ArtemisApi;
import com.detection.hikvision.utils.HaiKangHttpUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author LH
 * @date 2022/9/14 17:42
 */
@Service
public class ArtemisRegionsServiceImpl implements ArtemisRegionsService {

    @Resource
    private CusArtemisConfig cusArtemisConfig;

    /**
     * 获取跟区域信息
     *
     * @return
     */
    @Override
    public RootRegionsResultView getRootRegions() {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("treeCode", "0");
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 1000);
        String send = new HaiKangHttpUtils().send(cusArtemisConfig, jsonBody, ArtemisApi.GET_ROOT_REGIONS);
        return !ObjectUtils.isEmpty(send) ? JSON.parseObject(send,RootRegionsResultView.class) : null;
    }

    /**
     * 根据资源类型获取跟区域树信息
     * @return
     */
    @Override
    public JSONArray getRegionsTree(String resourceType,String regionName) {
        JSONObject jsonObject1 =new JSONObject();
        jsonObject1.put("resourceType",resourceType);
        jsonObject1.put("pageNo",1);
        jsonObject1.put("pageSize",1000);
        jsonObject1.put("authCodes", new String[]{"view"});
        if(!Objects.isNull(regionName)){
            jsonObject1.put("regionName",regionName);
        }
        String send = new HaiKangHttpUtils().send(cusArtemisConfig, jsonObject1, ArtemisApi.GET_REGION_LIST);
        if (Objects.isNull(send)){
            return new JSONArray();
        }
        JSONObject result = JSON.parseObject(send);
        JSONObject data =result.getJSONObject("data");
        System.out.println(send);
        return data.getJSONArray("list");
    }

}
